using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UwinEducation.Application.Dtos;
using UwinEducation.Application.Common.Interfaces;
using UwinEducation.Shared.Models;
using UwinEducation.API.Attributes;

namespace UwinEducation.API.Controllers
{
    /// <summary>
    /// 角色管理控制器
    /// </summary>
    [Authorize]
    //[RequirePermission("system.role")] // 指定所需的权限

    [ApiController]
    [Route("api/[controller]/[action]")]
    public class RoleController : ControllerBase
    {
        private readonly IRoleService _roleService;

        public RoleController(IRoleService roleService)
        {
            _roleService = roleService;
        }

        /// <summary>
        /// 获取角色分页列表
        /// </summary>
        [HttpPost]
        public async Task<ApiResponse<PagedResult<RoleDto>>> GetPagedList([FromBody] PageRequest pageRequest)
        {
            var result = await _roleService.GetPagedListAsync(pageRequest);
            return ApiResponse<PagedResult<RoleDto>>.Ok(result);
        }

        /// <summary>
        /// 获取角色详情
        /// </summary>
        [HttpPost]
        public async Task<ApiResponse<RoleDto>> GetById([FromBody] IdRequest request)
        {
            var role = await _roleService.GetByIdAsync(request.Id);
            if (role != null)
            {
                role.PermissionCodes = await _roleService.GetPermissionCodesAsync(request.Id);
            }
            return ApiResponse<RoleDto>.Ok(role);
        }

        /// <summary>
        /// 创建角色
        /// </summary>
        [HttpPost]
        [RequirePermission("system.role")] // 指定所需的权限
        public async Task<ApiResponse<RoleDto>> Create([FromBody] CreateRoleDto input)
        {
            var role = await _roleService.CreateAsync(input);
            return ApiResponse<RoleDto>.Ok(role);
        }

        /// <summary>
        /// 更新角色
        /// </summary>
        [HttpPost]
        [RequirePermission("system.role")] // 指定所需的权限
        public async Task<ApiResponse<object>> Update([FromBody] UpdateRoleDto input)
        {
            await _roleService.UpdateAsync(input);
            return ApiResponse<object>.Ok(new object());
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        [HttpPost]
        [RequirePermission("system.role")] // 指定所需的权限
        public async Task<ApiResponse<object>> Delete([FromBody] IdRequest request)
        {
            await _roleService.DeleteAsync(request.Id);
            return ApiResponse<object>.Ok(new object());
        }

        /// <summary>
        /// 分配菜单权限
        /// </summary>
        [HttpPost]
        public async Task<ApiResponse<object>> AssignMenus([FromBody] AssignMenusDto input)
        {
            await _roleService.AssignMenusAsync(input);
            return ApiResponse<object>.Ok(new object());
        }
    }
}